package com.ruitu.mall_98_2.order;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.Config;
import com.beanu.arad.base.my_base.MyRefreshBaseActivity;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.bumptech.glide.Glide;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.mvp.contract.MyGroupBookingContract;
import com.ruitu.mall_98_2.mvp.model.MyGroupBookingModel;
import com.ruitu.mall_98_2.mvp.presenter.MyGroupBookingPresenter;
import com.ruitu.router_module.A_Rutils;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.order.SmallOrder;

import java.util.ArrayList;
import java.util.List;

public class MyGroupBookingActivity extends MyRefreshBaseActivity<MyGroupBookingPresenter, MyGroupBookingModel> implements MyGroupBookingContract.View {

    private DataAdapter adapter;
    private List<Integer> skipId = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHeadTitle("正在拼团");
        skipId.add(R.id.header);
    }

    @Override
    protected void initViews() {
        super.initViews();
        setLoadMoreEnabled(false);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_my_group_booking;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return findViewById(R.id.progress);
    }

    @Override
    protected void reqData() {
        reReq();
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new DataAdapter(this);
        return adapter;
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.lRecyclerView);
    }

    @Override
    protected void reqFirst() {
        reReq();
    }

    @Override
    protected void reqMore() {

    }

    @Override
    protected void reReq() {
        showProgress();
        mPresenter.myCollage(AppHolder.instance().getUser().getAccountId());
    }

    @Override
    public void setData(List<SmallOrder> collages) {
        refreshComplete();
        if (isDataEmpty(collages)) {
            showEmptyView(skipId);
        } else {
            adapter.setDataList(collages);
            showContent(null);
        }
    }

    @Override
    public void setFail() {
        refreshComplete();
        showError(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reReq();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        reReq();
    }

    class DataAdapter extends ListBaseAdapter<SmallOrder> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_my_group_booking;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            ImageView iv_commodity = holder.getView(R.id.iv_commodity);
            ImageView iv_company_logo = holder.getView(R.id.iv_company_logo);
            TextView tv_commodity_name = holder.getView(R.id.tv_commodity_name);
            TextView tv_commodity_info = holder.getView(R.id.tv_commodity_info);
            TextView tv_commodity_price = holder.getView(R.id.tv_commodity_price);
            TextView tv_commodity_num = holder.getView(R.id.tv_commodity_num);
            TextView tv_pay_type_adj = holder.getView(R.id.tv_pay_type_adj);
            TextView tv_actually_paid = holder.getView(R.id.tv_actually_paid);
            TextView tv_company_name = holder.getView(R.id.tv_company_name);
            TextView tv_pay_model = holder.getView(R.id.tv_pay_model);
            LinearLayout ll_small_item = holder.getView(R.id.ll_small_item);
            final SmallOrder collage = mDataList.get(position);
            Glide.with(mContext).load(Config.BASE_URL + collage.getGoodsUrl()).apply(Arad.opts).into(iv_commodity);
            Glide.with(mContext).load(Config.BASE_URL + collage.getCompanyLogo()).apply(Arad.opts_shop).into(iv_company_logo);
            tv_commodity_name.setText(collage.getGoodsName());
            tv_commodity_info.setText(collage.getSkuContent());
            tv_commodity_price.setText("￥" + collage.getGoodsPrice());
            tv_commodity_num.setText("x" + collage.getQuantity());
            tv_company_name.setText(collage.getCompanyName());
            if (!TextUtils.isEmpty(collage.getFreight()) && Double.parseDouble(collage.getFreight()) > 0) {
                tv_actually_paid.setText("实付款:" + collage.getOrderTotal() + "元" + "(包含运费" + collage.getFreight() + "元)");
            } else {
                tv_actually_paid.setText("实付款:" + collage.getOrderTotal() + "元");
            }

            if (TextUtils.equals("3", collage.getOrderType())) {
                tv_pay_model.setText("进行中");
            }
            final Bundle bundle = new Bundle();
            ll_small_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    bundle.putSerializable("smallOrder", collage);
                    startActivity(OrderDetailsActivity.class, bundle);
                }
            });

            tv_company_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        bundle.putString("companyId", collage.getCompanyId());
                        A_Rutils.startActivity(mContext, "com.ruitu.mall_98_1.product.activity.ShopDetailActivity", bundle);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
